package com.erp.activityIKP

import android.content.Context
import android.util.Log
import com.erp.activityKP.SeekKP
import com.erp.activityKView.SeekKView
import com.erp.bean.BypassBean
import com.erp.Interface.OnUserDataInterface
import com.erp.util.Httputil
import com.erp.util.JsonUtil
import com.erp.util.Sputil
import com.erp.activity.SeekActivity
import com.erp.activity.SeekActivity.Companion.SEEK_HETONG
import com.erp.activity.SeekActivity.Companion.SEEK_KEHUMING
import com.erp.activity.SeekActivity.Companion.SEEK_ORDER
import com.erp.activity.SeekActivity.Companion.SEEK_SHEJISHI
import com.erp.activity.SeekActivity.Companion.SEEK_TEL
import com.erp.activity.SeekActivity.Companion.SEEK_XIAOSHOUYUAN
import com.erp.activity.SeekActivity.Companion.SEEK_ZMD
import org.json.JSONArray
import org.json.JSONObject

/**
 * Created by Administrator on 2018/5/22/022.
 * 作者：刘洋
 */
class ISeekP(val KView : SeekKView,val context : Context,val mlist : MutableList<BypassBean>,val seek : Int) : SeekKP{
    override fun gainData(key: String) {
    }


    var page = 0
    var pagesize = 100

    val editor = Sputil.editor("seek")
    val sp = Sputil.sp("seek")
    var seeksp =""


    override fun xiala() {
        Log.d("SEEKS","xiala")
        when(seek){
            SeekActivity.SEEK_XIAOSHOUYUAN, SeekActivity.SEEK_SHEJISHI -> {
                page=0
                HttpName(1)
            }
            SeekActivity.SEEK_KEHUMING, SeekActivity.SEEK_TEL, SeekActivity.SEEK_ORDER, SeekActivity.SEEK_HETONG, SeekActivity.SEEK_ZMD -> {
                KView.xialaOk()
            }
        }
    }

    override fun shangla() {
        when(seek){
            SeekActivity.SEEK_XIAOSHOUYUAN, SeekActivity.SEEK_SHEJISHI -> {
                page++
                HttpName(2)
            }
            SeekActivity.SEEK_KEHUMING, SeekActivity.SEEK_TEL, SeekActivity.SEEK_ORDER, SeekActivity.SEEK_HETONG, SeekActivity.SEEK_ZMD -> {
                KView.shanglaOk()
            }
        }
    }

    override fun getData() {
        when(seek){
            SEEK_XIAOSHOUYUAN,SEEK_SHEJISHI -> {
                page=0
                HttpName(3)
            }
            SEEK_KEHUMING,SEEK_TEL,SEEK_ORDER,SEEK_HETONG,SEEK_ZMD -> {
                mlist.clear()
                huoqusp("seek"+seek)
                KView.getDataOk()
            }
        }
    }

    override fun addsp(s: String) {
        val b = mlist.none { it.name==s }
        if (b) {
                if (mlist.size!=0) {
                    seeksp=""
                    if(mlist.size>10){
                        for (i in 0..4) {
                            mlist.removeAt(i)
                        }
                    }
                    for(i in mlist.indices){
                        if(mlist[i].name.trim() != ""){
                            when (i) {
                                mlist.size - 1 -> seeksp =seeksp+ ","+ mlist[i].name
                                0 -> seeksp = mlist[0].name
                                else -> seeksp = seeksp+ "," + mlist[i].name
                            }
                        }
                    }
                    seeksp = seeksp + "," + s
                } else {
                    seeksp = s
                }
            if(sp.getBoolean("seeks"+seek,false)){
                editor.putString("seek" + seek, "")
                editor.putBoolean("seeks"+seek,true)
            }else {
                editor.putString("seek" + seek, seeksp)
            }
            editor.commit()
        }
    }
    override fun cleansp() {
        editor.putString("seek"+seek, "")
        editor.commit()
        mlist.clear()
        KView.getDataOk()
    }

    fun HttpName(code : Int){
        Httputil.getHttpBypass(context, page, pagesize, object : OnUserDataInterface {
            override fun onUserData(data: String) {
                when(code){
                    1 -> {
                        mlist.clear()
                        jsonData(data)
                        KView.xialaOk()
                    }
                    2 -> {
                        jsonData(data)
                        KView.shanglaOk()
                    }
                    3 -> {
                        mlist.clear()
                        jsonData(data)
                        KView.getDataOk()
                    }
                }
            }

            override fun onNot(throwable: String) {
                when(code){
                    1 -> KView.xialaNo()
                    2 -> KView.shanglaNo()
                    3 -> KView.getDataNo()
                }
            }
        })
    }
    fun jsonData(data : String){
        Log.d("TAAAG","data="+data)
        try {
            val js = JSONArray(data)
            for (i in 0 until js.length()) {
                val jo = js.get(i) as JSONObject
                val bypassBean = BypassBean()
                bypassBean.uid = JsonUtil.getvolue(jo,"uid")
                bypassBean.name = JsonUtil.getvolue(jo,"father_name")+"-"+ JsonUtil.getvolue(jo,"name")
                mlist.add(bypassBean)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
    fun huoqusp(s : String){
        seeksp = sp.getString(s, "")
        val seekarray = seeksp.split(",")
        for(i in 0 until seekarray.size){
            if(seekarray[i].trim() != "") {
                val bypassBean = BypassBean()
                bypassBean.name = seekarray[i]
                mlist.add(bypassBean)
            }
        }
    }
}